const file = require('../models/model/file');
const files = require('../models/model/file');

/**
 * 创建文件
 * @param {文件名} name 
 * @param {内容} content 
 * @param {文件的类型} type 富文本框,脑图,表格 
 *  @param {文件夹的id} folderId 富文本框,脑图,表格 
 * isDelete 默认为0 默认可以删除
 */
exports.createFiles = async (name, content, type, folderId) => {
    const ins = await files.create({
        name,
        content,
        isDelete: 0,
        type,
        folderId
    })
    if (ins) {
        return ins.toJSON();
    }
    return null;
}


/**
 * 获取文件列表
 * @param {第几页} page 
 * @param {一页几条} limit 
 * @param {文件夹的id} folderId 
 * @returns 
 */
exports.findFiles = async (page = 1, limit = 100, folderId = "") => {
    const ins = await files.findAll({
        offset: (page - 1) * limit,
        limit: +limit,
        where: {
            folderId
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 
 * @param {文件夹id} folderId 
 * @returns 查询所有文件
 */
exports.findFilesAllCount = async (folderId) => {
    const ins = await files.findAll({
        where: {
            folderId
        }
    })
    if (ins) {
        return ins;
    }
    return null;
}

/**
 * 查询单个文件
 */
exports.findFile = async (folderId, fileId) => {
    const ins = await files.findOne({
        where: {
            id: fileId,
            folderId
        }
    })
    return ins;
}

/**
 * 
 * @param {文件夹id} folderId 
 * @param {文件id} fileId 
 * @param {名字} name 
 * @param {内容} content 
 */
exports.updateFile = async (folderId, fileId, name, content) => {
    return await files.update(
        {
            name,
            content
        },
        {
            where: {
                id: fileId,
                folderId
            }
        }
    );
}

// 删除单个文件
exports.deleteFile = async (fileId) => {
    const ins = await files.destroy(
        {
            where: {
                id: fileId,
            }
        }
    )
    if (ins) {
        return ins;
    }
    return null;
}